package com.java.test.agent.module.info;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.test.agent.constant.AgentCodeConst;
import com.java.test.agent.module.apply.dto.AgentApplyDTO;
import com.java.test.agent.module.apply.dto.AgentApplyQueryDTO;
import com.java.test.agent.module.apply.entity.AgentApplyEntity;
import com.java.test.agent.module.apply.vo.AgentApplyVO;
import com.java.test.agent.module.feescale.entity.AgentFeeScaleEntity;
import com.java.test.agent.module.feescale.vo.AgentFeeScaleVO;
import com.java.test.agent.module.info.dto.AgentCensusDTO;
import com.java.test.agent.module.info.dto.AgentInfoDTO;
import com.java.test.agent.module.info.dto.AgentInfoQueryDTO;
import com.java.test.agent.module.info.dto.AgentInfoUpdateDTO;
import com.java.test.agent.module.info.entity.AgentInfoEntity;
import com.java.test.agent.module.info.vo.AgentCensusVO;
import com.java.test.agent.module.info.vo.AgentInfoVO;
import com.java.test.dto.PageResultDTO;
import com.java.test.dto.ResponseDTO;
import com.java.test.util.SmartBeanUtil;
import com.java.test.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author lijie1
 * @date 2020/6/30 0030
 */
@Service
public class AgentInfoService {
    private AgentInfoDao agentInfoDao;
    @Autowired
    public void setAgentInfoDao(AgentInfoDao agentinfoDao) {
        this.agentInfoDao = agentinfoDao;
    }

    /**
     * 分页查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<PageResultDTO<AgentInfoVO>> getSelectByList(AgentInfoQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<AgentApplyEntity> entityCount = agentInfoDao.selectByList();
        page.setTotal(entityCount.size());
        List<AgentInfoEntity> entityList = agentInfoDao.getSelectByList(queryDTO);
        PageResultDTO<AgentInfoVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, entityList, AgentInfoVO.class);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 解冻
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public ResponseDTO<PageResultDTO<AgentInfoVO>> passByIds(AgentInfoDTO dto) {
        List<AgentInfoEntity> entityList = agentInfoDao.getByIds(dto.getIds());
        if (entityList.size()<=0) {
            return ResponseDTO.wrap(AgentCodeConst.NOT_EXIST_INFO);
        }
        agentInfoDao.updateById(0,dto.getIds());
        return ResponseDTO.succ();
    }

    /**
     * 冻结
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public ResponseDTO<PageResultDTO<AgentInfoVO>> failByIds(AgentInfoDTO dto) {
        List<AgentInfoEntity> entityList = agentInfoDao.getByIds(dto.getIds());
        if (entityList.size()<=0) {
            return ResponseDTO.wrap(AgentCodeConst.NOT_EXIST_INFO);
        }
        agentInfoDao.updateById(1,dto.getIds());
        return ResponseDTO.succ();
    }

    /**
     * 查询费率
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public ResponseDTO<AgentInfoVO> selectScaleByIds(AgentInfoDTO dto) {
        AgentInfoEntity entity = agentInfoDao.getEntity(dto);
        AgentInfoVO configDTO = SmartBeanUtil.copy(entity, AgentInfoVO.class);
        return ResponseDTO.succData(configDTO);
    }

    /**
     * 更新费率
     * @author lijie1
     * @date 2020/7/4 0004
     */
    public ResponseDTO<AgentInfoVO> updateScale(AgentInfoUpdateDTO dto) {
        agentInfoDao.updateScale(dto);
        return ResponseDTO.succ();
    }

    /**
     * 统计代理商信息
     * @author lijie1
     * @date 2020/7/4 0004
     */
    public ResponseDTO<AgentCensusVO> getAgentCensus(AgentCensusDTO dto) {
        AgentCensusVO obj = new AgentCensusVO();
        List<Map<String,Object>> list = agentInfoDao.getAgentCensusByNew(dto);
        for (Map<String,Object> map:list) {
            obj.setNewAgentNum(map.get("num").toString());
        }
        list = agentInfoDao.getAgentCensusByNew(new AgentCensusDTO());
        for (Map<String,Object> map:list) {
            obj.setAgentNum(map.get("num").toString());
        }
        obj.setAgentFunds("99999");
        return  ResponseDTO.succData(obj);
    }
}
